"""
新版 RSI策略v1 单周期交易策略
last update: 2024年12月19日
"""
# from typing import Literal
# from Cryptodome.SelfTest.Hash.test_HMAC import default_hash
from pythongo.classdef import OrderData
from pythongo.base import BaseParams, BaseState, BaseStrategy, Field
from pythongo.classdef import KLineData, TickData
from pythongo.core import KLineStyleType
from pythongo.utils import KLineGenerator

class Params(BaseParams):
    """RSI指标策略version1.0版本  单周期交易策略
           修复问题：
               1 启动策略，会找不到历史数据导致程序信号异常
               2.k线周期管理
               3.当前文件多周期k线数据不正确  需要验证
               4.相同的交易信号只交易一次
               5.下单逻辑
       """
    auther = '尘心|wx:17630013170'
    className = 'New_RSI_V1'

    """参数映射模型"""
    exchange: str = Field(default="", title="交易所代码")
    instrument_id: str = Field(default="", title="合约代码")
    # order_price: int | float = Field(default=0, title="报单价格")
    # order_direction: Literal["buy", "sell"] = Field(default="buy", title="报单方向")
    kline_style: KLineStyleType = Field(default="M1", title="K 线周期")
    trade_type: str = Field(default="", title="交易方向") #L 多(买点入场，卖点出场) S 空(卖点入场，买点出场)
    order_volume: int = Field(default=1, title="报单手数")
    volume_num: int = Field(default=1, title="总手数")
    # fast_period: int = Field(default=5, title="快均线周期", ge=2)
    # slow_period: int = Field(default=20, title="慢均线周期")



class State(BaseState):
    """状态映射模型"""
    order_id: int | None = Field(default=None, title="报单编号")
    trading: str = Field(default="交易中", title="交易状态")
    pos: str = Field(default="交易中", title="持仓")


class New_RSI(BaseStrategy):
    def __init__(self) -> None:
        super().__init__()
        self.params_map = Params()
        self.state_map = State()

        # RSI参数及区间
        self.R = 6
        # self.S = 12
        # self.I = 24
        self.rup = 80
        self.rdm = 20

        # 参数变量
        self.current_rsi = 0  # 当前N分钟 K 线rsi数值
        self.prev_rsi = 0  # 上一根N分钟 K 线rsi数值

    def on_order(self, order: OrderData) -> None:
        super().on_order(order)
        self.output("报单信息：", order)

    def on_start(self) -> None:
        self.kline_generator = KLineGenerator(
            real_time_callback=self.real_time_callback,
            callback=self.callback,
            exchange=self.params_map.exchange,
            instrument_id=self.params_map.instrument_id,
            style=self.params_map.kline_style
        )
        self.kline_generator.push_history_data()
        super().on_start()

    def on_tick(self, tick: TickData) -> None:
        """收到行情 tick 推送"""
        super().on_tick(tick)
        self.kline_generator.tick_to_kline(tick)

    def on_stop(self) -> None:
        super().on_stop()
        self.output("我的第一个策略暂停了")

    def calc_indicator(self) -> None:
        """计算指标数据"""
        self.slow_ma = self.kline_generator.producer.sma(
            timeperiod=self.params_map.slow_period
        )

        self.fast_ma = self.kline_generator.producer.sma(
            timeperiod=self.params_map.fast_period
        )

    def callback(self, kline: KLineData) -> None:
        """接受 K 线回调"""
        self.calc_indicator()

    def real_time_callback(self, kline: KLineData) -> None:
        """使用收到的实时推送 K 线来计算指标并更新线图"""
        self.callback(kline)